/*
 * Filename:    IndicationRegionOrgHelp.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月18日
 */
package com.paic.mhis.hcpms.quota.settlement.biz.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiDeptDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiMedicalOrgDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiSociationDTO;
import com.paic.mhis.hcpms.quota.settlement.common.Constants;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationBeanBaseDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationRegionOrgDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationRegionOrgPageBean;
import com.paic.mhis.hcpms.quota.settlement.dto.MedicalOrgDTO;

@Component("indicationRegionOrgHelp")
public class IndicationRegionOrgHelp {

	public IndicationRegionOrgDTO processIndicationDeptment(List<KpiDeptDTO> kpiDeptList, MedicalOrgDTO bean,
			String endTime) {
		IndicationRegionOrgDTO regionOrgBean = new IndicationRegionOrgDTO();
		String deptCode = bean.getDeptCode();
		regionOrgBean.setType("depart");
		regionOrgBean.setId(bean.getDeptCode());
		regionOrgBean.setName(bean.getDeptName());
		regionOrgBean.setDeptCode(deptCode);
		regionOrgBean.setRegionCode(bean.getRegionCode());
		regionOrgBean.setOrgCode(bean.getOrgCode());
		for (KpiDeptDTO kpiDept : kpiDeptList) {
			if (deptCode.equals(kpiDept.getDeptCode())) {
				// 判断是否是最后一个月的数据 是则获取同比环比值
				String kpiTime = kpiDept.getKpiTime();
				// 赋值同比，环比
				if (endTime.equals(kpiDept.getKpiTime())) {
					// 同比
					double periodRatio = kpiDept.getPeriodRatio() != null ? kpiDept.getPeriodRatio().doubleValue()
							: 0.00;
					regionOrgBean.setYoy(periodRatio);

					// 环比
					double chainRatio = kpiDept.getChainRatio() != null ? kpiDept.getChainRatio().doubleValue() : 0.00;
					regionOrgBean.setMom(chainRatio);
				}
				// 获取月份
				String month = IndicationHospitalHelp.getMonth(kpiTime);
				// 获取月份的值
				this.makeMonthValue(regionOrgBean, month, kpiDept.getKpiValue());
			}
		}
		return regionOrgBean;
	}

	public IndicationRegionOrgDTO processIndicationMedicalOrg(List<KpiMedicalOrgDTO> medicalOrgList,
			MedicalOrgDTO bean, String endTime) {
		IndicationRegionOrgDTO regionOrgBean = new IndicationRegionOrgDTO();
		String orgCode = bean.getOrgCode();
		regionOrgBean.setType("org");
		regionOrgBean.setId(orgCode);
		regionOrgBean.setName(bean.getMedicalName());
		regionOrgBean.setAreaCode(bean.getAreaCode());
		regionOrgBean.setRegionCode(bean.getRegionCode());
		regionOrgBean.setOrgCode(orgCode);

		for (KpiMedicalOrgDTO kpiOrg : medicalOrgList) {
			if (orgCode.equals(kpiOrg.getMedicalOrgCode())) {
				// 判断是否是最后一个月的数据 是则获取同比环比值
				String kpiTime = kpiOrg.getKpiTime();
				// 赋值同比，环比
				if (endTime.equals(kpiOrg.getKpiTime())) {
					// 同比
					double periodRatio = kpiOrg.getPeriodRatio() != null ? kpiOrg.getPeriodRatio().doubleValue() : 0.00;
					regionOrgBean.setYoy(periodRatio);

					// 环比
					double chainRatio = kpiOrg.getChainRatio() != null ? kpiOrg.getChainRatio().doubleValue() : 0.00;
					regionOrgBean.setMom(chainRatio);
				}
				// 获取月份
				String month = IndicationHospitalHelp.getMonth(kpiTime);
				// 获取月份的值
				this.makeMonthValue(regionOrgBean, month, kpiOrg.getKpiValue());
			}
		}
		return regionOrgBean;
	}

	/**
	 * 
	 * @param sociationList
	 * @param bean
	 * @return
	 */
	public IndicationRegionOrgDTO processSocial(List<KpiSociationDTO> sociationList, MedicalOrgDTO dto, String endTime) {

		IndicationRegionOrgDTO regionOrgBean = new IndicationRegionOrgDTO();
		String regionCode = dto.getRegionCode();
		regionOrgBean.setType("region");
		regionOrgBean.setId("");
		regionOrgBean.setName(dto.getAreaName());
		regionOrgBean.setAreaCode(dto.getAreaCode());
		regionOrgBean.setRegionCode(regionCode);

		for (KpiSociationDTO kpiOrg : sociationList) {
			if (regionCode.equals(kpiOrg.getSociationCode())) {
				// 判断是否是最后一个月的数据 是则获取同比环比值
				String kpiTime = kpiOrg.getKpiTime();
				// 赋值同比，环比
				if (endTime.equals(kpiOrg.getKpiTime())) {
					// 同比
					double periodRatio = kpiOrg.getPeriodRatio() != null ? kpiOrg.getPeriodRatio().doubleValue() : 0.00;
					regionOrgBean.setYoy(periodRatio);

					// 环比
					double chainRatio = kpiOrg.getChainRatio() != null ? kpiOrg.getChainRatio().doubleValue() : 0.00;
					regionOrgBean.setMom(chainRatio);
				}
				// 获取月份
				String month = IndicationHospitalHelp.getMonth(kpiTime);
				// 获取月份的值
				this.makeMonthValue(regionOrgBean, month, kpiOrg.getKpiValue());
			}
		}
		return regionOrgBean;
	}

	/**
	 * 获取医疗机构的数据
	 * 
	 * @param kpiMedicalOrgList
	 * @return
	 */
	public IndicationRegionOrgPageBean processMedicalOrg(List<KpiMedicalOrgDTO> kpiMedicalOrgList, String endTime) {

		IndicationRegionOrgPageBean pageBean = new IndicationRegionOrgPageBean();

		List<IndicationRegionOrgDTO> regionOrgBeanList = new ArrayList<IndicationRegionOrgDTO>();

		Set<String> orgCodeList = new HashSet<String>();
		// 获取当前医疗机构 编码集合
		for (KpiMedicalOrgDTO orgDto : kpiMedicalOrgList) {
			orgCodeList.add(orgDto.getMedicalOrgCode());
		}

		// 根据获取的医疗机构编码集合遍历数据
		for (String orgCode : orgCodeList) {
			IndicationRegionOrgDTO regionOrgBean = new IndicationRegionOrgDTO();
			int i = 0;
			for (KpiMedicalOrgDTO kpiOrg : kpiMedicalOrgList) {
				// 只需要一次赋值
				if (orgCode.equals(kpiOrg.getMedicalOrgCode()) && i == 0) {
					regionOrgBean.setType("org");
					regionOrgBean.setId(kpiOrg.getMedicalOrgCode());
					regionOrgBean.setName(kpiOrg.getMedicalOrgName());
					regionOrgBean.setRegionCode(kpiOrg.getSociationCode());
					regionOrgBean.setOrgCode(kpiOrg.getMedicalOrgCode());
					i++;
				}
				if (orgCode.equals(kpiOrg.getMedicalOrgCode())) {
					// 判断是否是最后一个月的数据 是则获取同比环比值
					String kpiTime = kpiOrg.getKpiTime();
					// 赋值同比，环比
					this.getOrgLevelRatio(regionOrgBean, kpiOrg, endTime);
					// 获取月份
					String month = IndicationHospitalHelp.getMonth(kpiTime);
					// 获取月份的值
					this.makeMonthValue(regionOrgBean, month, kpiOrg.getKpiValue());
				}

			}

			regionOrgBeanList.add(regionOrgBean);
			Collections.sort(regionOrgBeanList);
		}

		pageBean.setData(regionOrgBeanList);
		return pageBean;
	}

	/**
	 * 赋值 同比，环比
	 * 
	 * @param regionOrgBean
	 * @param kpiOrg
	 * @param endTime
	 */
	private void getOrgLevelRatio(IndicationRegionOrgDTO regionOrgBean, KpiMedicalOrgDTO kpiOrg, String endTime) {
		if (endTime.equals(kpiOrg.getKpiTime())) {
			// 同比
			if (null != kpiOrg.getPeriodRatio()) {
				regionOrgBean.setYoy(kpiOrg.getPeriodRatio().doubleValue());
			}
			// 环比
			if (null != kpiOrg.getChainRatio()) {
				regionOrgBean.setMom(kpiOrg.getChainRatio().doubleValue());
			}

		}
	}

	private void makeMonthValue(IndicationBeanBaseDTO bean, String month, BigDecimal monthValue) {
		if (Constants.month_01.equals(month)) {
			if (monthValue != null) {
				bean.setFirst(monthValue.doubleValue());
			} else {
				bean.setFirst(0d);
			}

		}
		if (Constants.month_02.equals(month)) {
			if (monthValue != null) {
				bean.setSecond(monthValue.doubleValue());
			} else {
				bean.setSecond(0d);
			}

		}
		if (Constants.month_03.equals(month)) {
			if (monthValue != null) {
				bean.setThird(monthValue.doubleValue());
			} else {
				bean.setThird(0d);
			}

		}
		if (Constants.month_04.equals(month)) {
			if (monthValue != null) {
				bean.setFourth(monthValue.doubleValue());
			} else {
				bean.setFourth(0d);
			}
		}
		if (Constants.month_05.equals(month)) {
			if (monthValue != null) {
				bean.setFifth(monthValue.doubleValue());
			} else {
				bean.setFifth(0d);
			}
		}
		if (Constants.month_06.equals(month)) {
			if (monthValue != null) {
				bean.setSixth(monthValue.doubleValue());
			} else {
				bean.setSixth(0d);
			}

		}
		if (Constants.month_07.equals(month)) {
			if (monthValue != null) {
				bean.setSeventh(monthValue.doubleValue());
			} else {
				bean.setSeventh(0d);
			}
		}
		if (Constants.month_08.equals(month)) {
			if (monthValue != null) {
				bean.setEighth(monthValue.doubleValue());
			} else {
				bean.setEighth(0d);
			}
		}
		if (Constants.month_09.equals(month)) {
			if (monthValue != null) {
				bean.setNinth(monthValue.doubleValue());
			} else {
				bean.setNinth(0d);
			}
		}
		if (Constants.month_10.equals(month)) {
			if (monthValue != null) {
				bean.setTenth(monthValue.doubleValue());
			} else {
				bean.setTenth(0d);
			}
		}
		if (Constants.month_11.equals(month)) {
			if (monthValue != null) {
				bean.setEleventh(monthValue.doubleValue());
			} else {
				bean.setEleventh(0d);
			}
		}
		if (Constants.month_12.equals(month)) {
			if (monthValue != null) {
				bean.setTwelfth(monthValue.doubleValue());
			} else {
				bean.setTwelfth(0d);
			}
		}
	}
}
